class TodoListController < ActionController::Base
  self.template_root = File.join(RAILS_ROOT, 'vendor/plugins/todo_list/app/views')
  require 'rake'
  layout 'todo_list'
  
  @@item = Struct.new(:filename, :lineno, :text)
  
  #TODO:
  def todo
    @items = find("TODO")
    @title = "To do list"
    render :action => "list"
  end
  
  #NOTE:
  def note
    @items = find("NOTE")
    @title = "Notes"
    render :action => "list"
  end
  
  #IDEA:
  def idea
    @items = find("IDEA")
    @title = "Ideas"
    render :action => "list"
  end  
  
  #REMOVE:
  def remove
    @items = find("REMOVE")
    @title = "Remove"
    render :action => "list"
  end  
  
  #DEBUG:
  def debug
    @items = find("DEBUG")
    @title = "Debug"
    render :action => "list"
  end  
  
  #TEST:
  def tests
    @items = find("TEST")
    @title = "Test"
    render :action => "list"
  end  
  
  #DEF:
  def functions
    @items = find("DEF")
    @title = "Methods"
    render :action => "list"
  end  
  
  #CHANGE:
  def change
    @items = find("CHANGE")
    @title = "Change"
    render :action => "list"
  end  
  
  #QUESTION:
  def questions
    @items = find("QUESTION")
    @title = "Questions"
    render :action => "list"
  end  
  
  #FIXME:
  def fixme
    @items = find("FIXME")
    @title = "FixMe"
    render :action => "list"
  end  
  
  
  
  # busca en los archivos .rb el texto que se le pasa por parametro.
  def find(txt)
    @todo_files = FileList['lib/**/*.rb', 'test/**/*.rb', 'app/**/*.rb'] 
#NOTE descomentar la linea seguiente para buscar tareas en los plugins. 
#Comentar la linea de arriba, sino da error.
    #@todo_files = FileList['lib/**/*.rb', 'test/**/*.rb', 'app/**/*.rb', 'vendor/plugins/**/*.rb'] 
    @items = Hash.new

    @todo_files.each do |file|
      lineno = 0
      list = File.readlines(file).inject([]) do |list, line|
        lineno += 1
        next list unless line =~ /#\s*#{txt}:?\s+(.*)/i
        list << @@item.new(file, lineno, $1)
        
      end
      @items[file] = list unless list.empty?
    end
    
    return @items 
  end

  
end
